package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String password=user.getPassword();
        String md5= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5);
        System.out.println(md5);
        QueryWrapper<User>queryWrapper=new QueryWrapper<>(user);
        User userDB=userMapper.selectOne(queryWrapper);
        if (userDB==null){
            return null;
        }
        String token= UUID.randomUUID().toString()
                .replace("-", "");
        return token;
    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        IPage<User>page=new Page<>(pageResult.getPageNum(),
                                   pageResult.getPageSize());
        boolean flag= StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.like(flag, "username", pageResult.getQuery());
        page=userMapper.selectPage(page,queryWrapper);
        pageResult.setTotal(page.getTotal())
                  .setRows(page.getRecords());
        return pageResult;
    }

    @Override
    @Transactional
    public void updateStatus(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void addUser(User user) {
        String password=user.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password)
                .setStatus(true);
//                .setCreated(new Date())
//                .setUpdated(user.getCreated());
        userMapper.insert(user);

    }

    @Override
    @Transactional
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }



    @Override
    public User getUserById(Integer id) {
        return   userMapper.selectById(id);

    }

    @Override
    @Transactional
    public void updateUser(User user) {
     userMapper.updateById(user);
    }

//    @Override
//    public PageResult getUserList(PageResult pageResult) {
//        long total=userMapper.selectCount(null);
//        int size= pageResult.getPageSize();
//        int start= (pageResult.getPageNum()-1)* size;
//        List<User>userList=userMapper.findListByPage(start,size);
//        pageResult.setTotal(total)
//                  .setRows(userList);
//        return pageResult;
//    }
}
